package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.project.QueryNonstandardProjectFormBean;
import com.info.zhiduoduo.admin.model.formbean.project.SaveNonstandardProjectFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.repository.entity.company.CompanyEntrustFile;
import com.info.zhiduoduo.repository.entity.project.NonstandardProject;
import com.info.zhiduoduo.repository.entity.system.SystemManager;
import com.info.zhiduoduo.repository.mapper.company.CompanyEntrustFileMapper;
import com.info.zhiduoduo.repository.mapper.project.NonstandardProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author tianyonghong
 * @create 2023/4/16
 */
@Service
public class NonstandardProjectService {

    @Resource
    private NonstandardProjectMapper nonstandardProjectMapper;
    @Resource
    private CompanyEntrustFileMapper companyEntrustFileMapper;

    public SmartResult saveOrUpdate(BaseSimpleFormBean<SaveNonstandardProjectFormBean> formBean){
        SaveNonstandardProjectFormBean save = formBean.getReqdata();

        // 查询合同状态 合同状态(0未上传 1有效 2已失效)
        int contractState = 0;
        CompanyEntrustFile entrustFile = companyEntrustFileMapper.getByCompid(save.getCompid());
        if (entrustFile != null) {
            if (!DateUtil.isExpired(entrustFile.getEntrustEndTime())) {
                contractState = 1;
            } else {
                contractState = 2;
            }
        }

        NonstandardProject nonstandardProject = new NonstandardProject();
        BeanUtils.copyProperties(save, nonstandardProject);
        nonstandardProject.setContractState(contractState);
        if (save.getId() == null || save.getId() == 0) {
            nonstandardProject.setCreateBy(formBean.getUserid());
        }
        nonstandardProject.insertOrUpdate();

        return SmartResult.success(nonstandardProject);
    }

    public SmartResult queryList(BaseSimpleFormBean<QueryNonstandardProjectFormBean> formBean) {
        Integer currUserId = formBean.getUserid();
        SystemManager manager = new SystemManager().selectById(currUserId);
        if (manager == null) return SmartResult.ofErrorParam();

        QueryNonstandardProjectFormBean bean = formBean.getReqdata();
        Page<NonstandardProject> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        nonstandardProjectMapper.queryList(currUserId, manager.getRoleid(), bean.getCompname(), bean.getCreateStartTime(), bean.getCreateEndTime());

        return SmartResult.ofPage(page);
    }

    public SmartResult complete(BaseSimpleFormBean<Integer> formBean){
        NonstandardProject np = nonstandardProjectMapper.selectById(formBean.getReqdata());
        if (np == null) {
            return SmartResult.fail(StatusMsg.StatusMsg_102);
        }
        np.setState(2).setCompleteTime(new Date()).updateById();
        return SmartResult.success(np);
    }

    public SmartResult del(BaseSimpleFormBean<Integer> formBean){
        NonstandardProject np = nonstandardProjectMapper.selectById(formBean.getReqdata());
        if (np == null) {
            return SmartResult.fail(StatusMsg.StatusMsg_102);
        }
        np.deleteById();
        return SmartResult.success();
    }
}
